<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>jmx-config-add</title>
	<%@ include file="../include/common.jsp"%>
	<%@ include file="../include/form.jsp"%>
	
	<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery/jquery-ui.min.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/js/select/jquery.multiselect.min.js"></script>
	<link rel="stylesheet"  type="text/css" href="${pageContext.request.contextPath}/style/default/select/jquery.multiselect.css" />
	<link rel="stylesheet"  type="text/css" href="${pageContext.request.contextPath}/style/default/jqueryui/jquery-ui.css" />
	
  <script type="text/javascript">
  
  function add(){
	  $.ajax({
		   type: "POST", 
		   url: "${pageContext.request.contextPath}/config/jmx-config!valideIpPort.action", //请求的action
		   data: "ip="+$("#ip").val()+"&port="+$("#port").val()+"&isValideFlag="+"true", //传的参数
		   dataType: 'json',
		   success: function(data){ //结果
			   if(data == true){
				   //提交
					 $.ajax({
						   type: "POST", 
						   url: "${pageContext.request.contextPath}/config/jmx-config!save.action", //请求的action
						   data: $("#myform").serialize(), //传的参数
						   dataType: 'json',
						   success: function(data){ //结果
							   if(data == true){
							   	    var isRef = '${isRef}';
							   	   if(isRef=='null'||isRef==''){
									   var pWin = getTopWin(window).currentWin["add-jmx-config"];
									   pWin.datagrid('${isRef}');
								   }
								   getTopWin(window).closejBox("add-jmx-config");
							   }
							   
						   }
					});
			   }else{
				   getTopWin(window).getMessageBox().alert("ip地址+端口号必须唯一存在","提示");
			   }
		   }
	});
  }
  
  $(function(){
	  $("#instType").change(function(){
		  if($("#instType").val() == 'weblogic'){
			  $("#port").val('9990');
		  }
		  if($("#instType").val() == 'iis'){
			  $("#port").val('80');
		  }
		  if($("#instType").val() == 'apache'){
			  $("#port").val('80');
		  }
		  if($("#instType").val() == 'websphere'){
			  $("#port").val('8090');
		  }
		  if($("#instType").val() == 'jboss'){
			  $("#port").val('1099');
		  }
		  if($("#instType").val() == 'tomcat'){
			  $("#port").val('8090');
		  }
	  });
	  $.formValidator.initConfig({formid:"myform"
          ,onerror:function(msg){}
          ,onsuccess:function(){
        	  		add();
          			return false;
          }
          });
	  
	  $("#ip").formValidator({onshow:"请输入正确格式的IP",onfocus:"请输入正确格式的IP",oncorrect:"输入正确"}).inputValidator({min:1,onerror:"不能为空"})
	  .functionValidator({
			    fun:function(val,elem){
			    	var reg = /^([0-9]|[1-9][0-9]|1\d\d|2[0-4]\d|25[0-5])\.([0-9]|[1-9][0-9]|1\d\d|2[0-4]\d|25[0-5])\.([0-9]|[1-9][0-9]|1\d\d|2[0-4]\d|25[0-5])\.([0-9]|[1-9][0-9]|1\d\d|2[0-4]\d|25[0-5])$/;
			    	if(val.match(reg) == null){
			    		return "格式不正确"
			        }
			        return true;
				}
	  });
	  $("#port").formValidator({onshow:"请输入数字 长度(1-5)",onfocus:"请输入数字 长度(1-5)",oncorrect:"输入正确"}).inputValidator({min:1,onerror:"不能为空"}).inputValidator({min:1,max:5,onerror:"长度范围（1-5）"})
	  .functionValidator({
			    fun:function(val,elem){
			    	 var reg = /^[0-9]+$/;
			    	    if(!reg.test(val)){
			    	        return "格式不正确";
			    	    }
			    	    return true;
				}
	  });
	  $("#userName").formValidator({onshow:"请输入字符串 长度(1-32)",onfocus:"请输入字符串 长度(1-32)",oncorrect:"输入正确"}).inputValidator({min:1,onerror:"不能为空"}).inputValidator({empty:{leftempty:false,rightempty:false,emptyerror:"两边不能有空格"},onerror:"不能为空"})
	  .functionValidator({
			    fun:function(val,elem){
			    	    if(val.replace(/[^\x00-\xff]/g, "xx").length > 32){
			    	        return "长度超出最大范围";
			    	    }
			    	    return true;
				}
	  });
	  $("#pwd").formValidator({onshow:"请输入字符串 长度(1-32)",onfocus:"请输入字符串 长度(1-32)",oncorrect:""}).inputValidator({empty:{leftempty:false,rightempty:false,emptyerror:"两边不能有空格"},onerror:"不能为空"})
	  .functionValidator({
			    fun:function(val,elem){
			    	    if(val.replace(/[^\x00-\xff]/g, "xx").length > 32){
			    	        return "长度超出最大范围";
			    	    }
			    	    return true;
				}
	  });
	  $("#pwd1").formValidator({onshow:"2次密码要一致",onfocus:"2次密码要一致",oncorrect:""}).compareValidator({desid:"pwd",operateor:"=",onerror:"2次密码不一致"});
	  $("#maxConn").formValidator({onshow:"请输入数字  长度范围(1-5)",onfocus:"请输入数字 长度范围(1-5)",oncorrect:"输入正确"}).inputValidator({min:1,onerror:"不能为空"}).inputValidator({min:1,max:5,onerror:"长度超出最大范围"});
 	  //如果是从新增资源页面访问
  	  var isResAdd = '${data}';
  	  if(isResAdd!=''){
  	  	$('#instType option').each(function(index,ob){
  	  		if(ob.text.indexOf('${data.resType}')!=-1){
  	  			$(ob).attr('selected','selected');
  	  			return false;
  	  		}
  	  	});
  	  
  	  	var ip = $('#ip');
  	  	ip.val('${data.ip}');
  	  	ip.focus();
  	  }
  });
  function closewin(){
	   getTopWin(window).closejBox("add-jmx-config");
  }

  function test(){
	  $("#testTable").css({"display":""});
	  $.ajax({
		   type: "POST", 
		   url: "${pageContext.request.contextPath}/config/config-conn!allProbe.action", //请求的action
		   data: '', //传的参数
		   dataType: 'json',
		   success: function(data){ //结果
			   var obj= document.getElementById("result");
			   obj.length=0;
			   if(data.length == 0){
				   obj.options[0]=new Option("没有正在运行的probe!");
			   }else{
				   for(var i = 0; i < data.length; i++){
					   obj.options[i]=new Option(data[i].name+"   测试中，请等待...",data[i].id);
				   }
				   var num = 0;
				   connTest(data,num);
			   }
		   }
		});
  }
  
  function connTest(probe,num){
		  $.ajax({
				type : "POST",
				url : "${pageContext.request.contextPath}/config/config-conn!connTest.action", //请求的action
				data : $("#myform").serialize()+"&type="+'JMX'+"&propId="+probe[num].id, //传的参数
				dataType : 'json',
				success : function(data) { //结果
					var obj= document.getElementById("result");
					if(data.flag == "true"){
						obj.options[num].text = "通过	"+probe[num].name+"	与	"+data.ip+"	连接成功！";
					}else{
						obj.options[num].text = "通过	"+probe[num].name+"	与	"+data.ip+"	连接失败！";
					}
					num++;
					if(num < probe.length){
						connTest(probe,num);
					}
				}
		});
  }
  
  </script>
  </head>
  
  <body class="padding-8">
    	<form action="" id="myform" name="myform" method="post">
    		<table class="table_eidt">
    			<tr>
	    			<td class="td_left" width="80px"><span class="span_alert">*</span>服务器IP</td>
	    			<td class="td_right"><input type="text" name="ip" id="ip" class="input_text">
	    			<span id="ipTip" style="width:150px"></span></td>
    			</tr>
    			<tr>
	    			<td class="td_left"><span class="span_alert">*</span>应用类型</td>
	    			<td class="td_right"><select name="instType" id="instType" style="width: 225px">
			          <option value="weblogic">weblogic</option>
			          <option value="iis">iis</option>
			          <option value="apache">apache</option>
			          <option value="websphere">websphere</option>
			          <option value="jboss">jboss</option>
			          <option value="tomcat">tomcat</option>
			        </select>
	    			<span id="protocolTip" style="width:150px"></span></td>
    			</tr>
    			<tr>
	    			<td class="td_left"><span class="span_alert">*</span>端口号</td>
	    			<td class="td_right"><input type="text" name="port" id="port" value="9990" class="input_text">
	    			<span id="portTip" style="width:150px"></span></td>
    			</tr>
    			<tr>
	    			<td class="td_left"><span class="span_alert">*</span>帐号</td>
	    			<td class="td_right"><input type="text" name="userName" id="userName" class="input_text">
	    			<span id="userNameTip" style="width:150px"></span></td>
    			</tr>
    			<tr>
	    			<td class="td_left">密码</td>
	    			<td class="td_right"><input type="password" name="pwd" id="pwd" class="input_text">
	    			<span id="pwdTip" style="width:150px"></span></td>
    			</tr>
    			<tr>
	    			<td class="td_left">确认密码</td>
	    			<td class="td_right"><input type="password" name="pwd1" id="pwd1" class="input_text">
	    			<span id="pwd1Tip" style="width:150px"></span></td>
    			</tr>
    			<tr>
					<td class="td_left">
						<span class="span_alert">*</span>最大连接数
					</td>
					<td class="td_right">
						<input type="text" name="maxConn" id="maxConn" value="2"
							class="input_text">
						<span id="maxConnTip" style="width: 190px"></span>
					</td>
				</tr>
    		</table>
    		<div align="center" style="margin-top:9px;">
    		<input type="button" id="connTest" name="connTest" value="测试" class="btn-4" onclick="test();">
	    		<input type="submit" id="button" name="button" value="保存" class="btn-4">
	    		<input type="reset" class="btn-4">
	    		<input type="button" value="取消" onclick="closewin();" class="btn-4">
    		</div>
    		<table class="table_eidt" style="margin-top:9px;display: none;" id="testTable">
    			<tr>
	    			<td class="td_left" width="100px">测试结果</td>
	    			<td class="td_right">
	    				<select multiple="multiple" id="result" style="width:390px; height: 40px;border-style:solid; border-width:1px; border-color:#cccccc;overflow: auto;"></select>
	    			</td>
    			</tr>
    		</table>
    	</form>
  </body>
</html>
